/*!
 * Ext JS Library 3.3.1
 * Copyright(c) 2006-2010 Sencha Inc.
 * licensing@sencha.com
 * http://www.sencha.com/license
 */
AddNoteWindow = Ext.extend(Ext.app.Module, {
    id: 'addnote-win',
    autostart: false,
    store: null,
    formPanel: null,
    slideUri: '',
    window: null,
    init: function() {
        this.launcher = {
            text: 'Add Note',
            iconCls: 'refwin-icon',
            handler: this.createWindow,
            scope: this
        };
    },
    
    onSubmitClick: function() {
    	var notetext = this.formPanel.get('notetext').getValue();
    	if (!notetext) {
    		alert('Please type any text!');
    		return;
    	}
    	var parentWindow = this.window;
    	ReferenceManager.addNote(this.slideUri, "", notetext, function(success) {
    		parentWindow.hide();
    		parentWindow.close();
    		alert('Note has been added.');
    	});
    },
    
    onCancelClick: function(){
    	this.window.hide();
    	this.window.close();
    },
    
    createWindow: function(slideUri) {
    	this.slideUri = slideUri;
    	var myId = 'addnote-win_'+new Date().getTime();
        var desktop = this.app.getDesktop();
        var ref = this;
        this.formPanel = new Ext.form.FormPanel({
        	items:[
        	    {
        	    	id: 'notetext',
        	    	fieldLabel: 'Text', 
        	    	xtype: 'textarea',
        	    	width: '100%',
        	    	height: '100%', 
        	    	editable: true,
        	    	triggerAction:'all'
        	    }
        	], 
        	buttons:[{
        		text: 'Add Note',
        		scope: ref,
        		handler: this.onSubmitClick
        	},{
        		text: 'Cancel',
        		scope: ref,
        		handler: this.onCancelClick
        	}]
        });
        this.window = desktop.createWindow({
            id: myId,
            title: 'Add a note for ' + slideUri,
            width: 400,
            height: 200,
            x: 340,
            y: 400,
            iconCls: 'refwin-icon',
            shim: false,
            animCollapse: false,
            border: false,
            constrainHeader: true,
            layout: 'fit',
            items: [ this.formPanel ]
        });
        this.window.show();
     }
});